(setq my/is-windows (eq system-type 'windows-nt)) ; Windows 系统
(setq my/is-linux (eq system-type 'gnu/linux)) ; Linux 系统
(setq my/is-manjaro (string-match "MANJARO" operating-system-release)) ; manjaro 系统
(setq my/is-termux (string= system-configuration "aarch64-unknown-linux-android")) ; termux
(setq my/is-userland (string= system-configuration "aarch64-unknown-linux-gnu")) ; userland
;; 代表图形界面
(setq my/is-terminal (not window-system))
;; 让鼠标滚动更好用
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1) ((control) . nil)))
(setq mouse-wheel-progressive-speed nil)
(setq my/enable-folding (not my/is-terminal))
(setq default-frame-alist '((fullscreen . maximized))) ;; 全屏
;; 禁止emacs写入自定义变量
(setq custom-file (expand-file-name "custom.el" user-emacs-directory)) 
;; (defvar my/font-height 150)
(defvar my/latex-preview-scale 1.3)
(defvar my/font-height 130)

;; 通过设置符号系统，可以解决 org2md时出现的\r换行问题
(setq-default buffer-file-coding-system 'utf-8-unix)

(setq-default
  ;; 禁用自动窗口垂直滚动
  auto-window-vscroll nil
  ;; 设置默认目录为用户的主目录
  default-directory "~"
  ;; 设置默认文本属性，包括行间距和行高
  default-text-properties '(line-spacing 0 line-height 1.2)
  ;; 设置窗口标题格式为缓冲区的名称
  frame-title-format "%b"
  ;; 当显示帮助窗口时，允许用户选择该窗口
  help-window-select t
  ;; 设置新创建的缓冲区的默认主要模式为 fundamental-mode
  initial-major-mode 'fundamental-mode
  ;; 禁用启动屏幕闪屏
  inhibit-startup-screen t
  ;; 允许在搜索期间移动光标
  isearch-allow-motion t
  ;; 允许惰性计数器用于搜索
  isearch-lazy-count t
  ;; 允许使用 `C-k` 删除整行
  kill-whole-line t
  ;; 在模式行上紧凑显示信息
  mode-line-compact t
  ;; 禁用备份文件的创建
  make-backup-files nil
  ;; 设置读取进程输出的最大值为 4MB
  read-process-output-max (* 4 1024 1024)
  ;; 在文件末尾强制添加换行符
  require-final-newline t
  ;; 设置滚动保守性为 1000
  scroll-conservatively 1000
  ;; 显示行尾的空格
  show-trailing-whitespace t
  ;; 设置系统时间的区域设置为 "C"
  system-time-locale "C"
  ;; 使用简短的回答
  use-short-answers t)

;; 设置字体大小
(defun my/set-font-size ()
  (interactive)
  (cond (my/is-termux
         (setq my/font-height 125
               my/latex-preview-scale 1.25))
        (t (pcase (display-pixel-height)
             ((pred (> 999))
              (setq my/font-height 130
                    my/latex-preview-scale 1.3))
             ((pred (> 1300))
              (setq my/font-height 160
                    my/latex-preview-scale 1.6))
             (_ (setq my/font-height 180
                      my/latex-preview-scale 1.8))))))
;; (my/set-font-size)

(defun my/set-window 
    ()
  (interactive)
  (unless my/is-termux ; 终端下以下设置不可用
    (tool-bar-mode 0) ; 禁用工具栏
    (scroll-bar-mode 0)) ; 禁用滚动条
  (menu-bar-mode 0) ; 禁用菜单栏
  )

(my/set-window)

(defun my/set-font 
    ()
  (interactive)
  (setq my/default-font "Google Sans Code") ;; fonts-firacode (apt) 或 ttf-fira-code (pacman)
  ;;    (setq my/default-font "Fira Code")
  (setq my/math-font "Latin Modern Math") ; texlive 字体
  ;;(setq my/math-font "Iosevka Comfy")
  (setq my/chinese-font
        (cond 
         (my/is-termux "Roboto")
         (my/is-manjaro "Iosevka Comfy") ;; ttf-arphic-ukai (pacman)
	 ;;            (my/is-linux "FZShengShiKaiShuS-DB-GB") ;; fonts-arphic-gkai00mp (apt)
         ;; (my/is-windows "FZShengShiKaiShuS-DB-GB")
         ;; (my/is-windows "TsangerJinKai03 W04")
          (my/is-windows "TsangerBL-GL W04")
	 ;; (my/is-windows "TsangerMXT W01")
         ;; (my/is-windows "Kaiti")
         ;;(my/is-windows "LXGW WenKai Screen")
         (t "Iosevka Comf")))
  (setq my/variable-pitch-font
        (cond 
         (my/is-linux "Iosevka Comfy Motion Duo") 
         (my/is-windows  "Iosevka Comfy Motion Duo")
         (t "Iosevka Comfy Motion Duo")))
  (setq my/fixed-pitch-font "Iosevka Comfy") ;; fonts-jetbrains-mono (apt) 或 ttf-jetbrains-mono (pacman)

  (if 
      (find-font 
       (font-spec :name my/default-font))
      (set-face-attribute 'default nil :font my/default-font :height my/font-height)  ; 默认字体及字号.
    (message 
     (format "cannot find %s for the default font" my/default-font))
    (set-face-attribute 'default nil :height my/font-height)) ; 找不到字体只设置字号

  (if 
      (find-font 
       (font-spec :name my/variable-pitch-font))
      (set-face-attribute 'variable-pitch nil :font my/variable-pitch-font :height my/font-height) ; 比例字体 
    (message 
     (format "cannot find %s for the variable pitch font"  my/variable-pitch-font)))

  (if 
      (find-font 
       (font-spec :name my/fixed-pitch-font))
      (set-face-attribute 'fixed-pitch nil :font my/fixed-pitch-font :height my/font-height) ; 等宽字体 
    (message 
     (format "cannot find %s for the fixed pitch font"  my/fixed-pitch-font)))

  (unless my/is-terminal ; 非图形界面 set-fontset-font
    (if 
        (find-font 
         (font-spec :name my/math-font))
        (set-fontset-font "fontset-default" 'mathematical my/math-font) ; 数学符号默认字体
      (message 
       (format "cannot find %s for the math font"  my/math-font)))
    (if 
        (find-font 
         (font-spec :name my/chinese-font))
        (set-fontset-font "fontset-default" 'han my/chinese-font) ; 中文默认字体
      (message 
       (format "cannot find %s for the chinese font"  my/chinese-font))))

  (setq inhibit-compacting-font-caches t) ; 汉字及 Unicode 显示问题
  (setq auto-window-vscroll nil)) ; 不根据行高做上下滚动调整

(defun my/set-org-font ()
  (interactive)
  ;; org 字体美化
  (require 'org-faces)
  (dolist (face '((org-level-1 . 1.05)
                  (org-level-2 . 1.0)
                  (org-level-3 . 1.0)
                  (org-level-4 . 1.0)
                  (org-level-5 . 1.0)
                  (org-level-6 . 1.0)
                  (org-level-7 . 1.0)
                  (org-level-8 . 1.0)))
    (set-face-attribute (car face) nil :weight 'medium :height (cdr face)))
  (set-face-attribute 'org-block nil :foreground nil :inherit 'fixed-pitch)
  (set-face-attribute 'org-block-begin-line nil :foreground nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-block-end-line nil :foreground nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-property-value nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-code nil   :inherit '(shadow fixed-pitch))
  (set-face-attribute 'org-verbatim nil  :inherit '(shadow fixed-pitch))
  (set-face-attribute 'org-special-keyword nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-meta-line nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-checkbox nil :inherit 'fixed-pitch)
  (set-face-attribute 'org-drawer nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-document-info-keyword nil :inherit '(font-lock-comment-face fixed-pitch))
  (set-face-attribute 'org-table nil :inherit 'fixed-pitch)
  (setq org-fontify-quote-and-verse-blocks t) ; 启用 org-qoute 变量为 quote 设置不同的字体
  (set-face-attribute 'org-quote nil :inherit 'fixed-pitch)
  (require 'org-indent)
  (set-face-attribute 'org-indent nil :inherit '(org-hide fixed-pitch)))

(with-eval-after-load 'org
  (setq my/title-font "Iosevka Comfy Motion Duo SmBd")
  (when (find-font (font-spec :name my/title-font))
    (set-face-attribute 'org-document-title nil 
                        :font my/title-font 
                        :weight 'bold 
                        :height 1.2)
    (dolist (level '(1 2 3 4 5 6 7 8))
      (set-face-attribute (intern (format "org-level-%d" level)) nil 
                          :font my/title-font 
                          :weight 'bold 
                          :height (* 1.05 (/ 9 (- 10 level)))))))

(my/set-font)
(my/set-org-font)

(defun my/set-line-number 
    ()
  (interactive)
  (column-number-mode) ; 显示列号
  (visual-line-mode 1) ; 自动断行
  (global-display-line-numbers-mode t) ; 显示行号
  ;;不显示行号的模式:
  (dolist 
      (mode '
       (term-mode-hook
	helpful-mode-hook
	eshell-mode-hook
	pdf-view-mode-hook
	eww-mode-hook))
    (add-hook mode 
              (lambda 
                () 
                (display-line-numbers-mode 0))))

  (setq display-line-numbers-type 'visual) ; 行号考虑自动断行
  ;;行号强制用等宽字体
  (unless 
      (or my/is-terminal 
          (not 
           (find-font 
            (font-spec :name my/default-font))))
    (set-face-attribute 'line-number nil :font my/default-font :height 0.9) 
    (set-face-attribute 'line-number-current-line nil :font my/default-font :height 0.9)))

(my/set-line-number)

(provide 'init-system)
